package com.wteam.dragon.shop.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.shop.mapper.ProductAttributeMapper;
import com.wteam.dragon.shop.mapper.ProductTypeMapper;
import com.wteam.dragon.shop.pojo.bean.ProductAttribute;
import com.wteam.dragon.shop.pojo.bean.ProductType;
import com.wteam.dragon.shop.service.ProductTypeService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.util.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author 陈晓辉
 * @Date 2020/6/29 16:01
 */
@Service
@RequiredArgsConstructor
public class ProductTypeServiceImpl implements ProductTypeService {
    private final ProductTypeMapper productTypeMapper;
    private final ProductAttributeMapper productAttributeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addProductType(String name) {
        ValidationUtil.isNull("name不能为空", name);
        ProductType productType = new ProductType();
        productType.setName(name);
        productTypeMapper.insert(productType);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delProductType(Long id) {
        ValidationUtil.isNull("id不能为空", id);
        productTypeMapper.deleteById(id);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editProductType(Long id, String name) {
        ValidationUtil.isNull("id不能为空", id);
        ValidationUtil.isNull("name不能为空", name);
        ProductType productType = new ProductType();
        productType.setId(id);
        productType.setName(name);
        productTypeMapper.updateById(productType);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更改成功");
        return resultMessage;
    }

    @Override
    public ResultMessage pageProductType(Page<ProductType> page) {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper<>();
        Page<ProductType> productTypePage = productTypeMapper.selectPage(page, queryWrapper);
        productTypePage.getRecords().forEach(productType -> {
            QueryWrapper<ProductAttribute> attributeQueryWrapper = new QueryWrapper<>();
            attributeQueryWrapper.lambda()
                    .eq(ProductAttribute::getProductTypeId, productType.getId())
                    .eq(ProductAttribute::getType, 0);
            productType.setAttributeCount(productAttributeMapper.selectCount(attributeQueryWrapper));
            attributeQueryWrapper.lambda()
                    .eq(ProductAttribute::getProductTypeId, productType.getId())
                    .eq(ProductAttribute::getType, 1);
            productType.setParamCount(productAttributeMapper.selectCount(attributeQueryWrapper));
        });
        ResultMessage resultMessage = new ResultMessage();
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("productTypePage", productTypePage);
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }
}
